package com.mj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/user")
public class Hello {
    @RequestMapping("/show")
    @ResponseBody //如果不加responsebody，则会直接找views/show.jsp，不会找show的服务器，
    public void show() {
        System.out.println("hello");
    }

    @RequestMapping("")
    @ResponseBody
    public void aa(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print("hell-aa");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用自带的out方法，由于是自带的，无编码方式，可能会导致中文乱码
     *
     * @param out
     */
    @RequestMapping(value = "bb.action")
    @ResponseBody
    public void bb(PrintWriter out) {
        out.print("hello-bb");
    }

    //使用多种配置方式，这几个名字都可以用
    @RequestMapping(value = {"c1", "c2", "c3"})
    @ResponseBody
    public String cc() {
        return "cc";
    }

    @RequestMapping("/dd")
    @ResponseBody
    public String dd() {
        System.out.println("helli-dd");
        return null;//如果没有responsebody，则会找默认视图，名字和dd一样
    }

    @RequestMapping(value = "/ee", method = RequestMethod.POST)
    @ResponseBody
    public void ee(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print(request.getParameter("name"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/ff")
    public String ff(Model m) {
        m.addAttribute("name", "fff团");
        return "ff";
    }

    @RequestMapping("/gg")
    public ModelAndView gg() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name","gg不要太吊");
        mv.setViewName("ff");//这里是设置视图，会直接寻找ff.jsp
        return mv;
    }
    @RequestMapping("/hh")
    public void hh(String callme,PrintWriter out){
        out.print(callme);
    }
    @RequestMapping("/ii") @ResponseBody
    public void ii(String name,int age){
        System.out.printf("姓名:%s,年龄:%d",name,age);//直接在地址栏输入名字和年龄
    }
    @RequestMapping(value = "/kk/{id}",method = RequestMethod.GET)//直接在地址栏kk后面输入/加id，会在kk.jsp视图里面打印出student_id
    public String kk(@PathVariable("id") int sid,Model m){//这里的pathvariable就相当于是注解，将sid的值赋给id,
     m.addAttribute("student_id",sid);//这种方式适用于伪装地址，在地址栏最后的.后面可随便输入字符，系统会忽略掉.后面的内容
        return "kk";
    }
    @RequestMapping(value = "/ll/{id}/{nam}",method = RequestMethod.GET) @ResponseBody//直接在地址栏kk后面输入/加id，会在kk.jsp视图里面打印出student_id
    public void ll(@PathVariable("id") int lid,@PathVariable("nam") String name,PrintWriter out){//这里的pathvariable就相当于是注解，将sid的值赋给id
    out.print(lid);
    out.print(name);
    out.flush();
    out.close();
    }
}
